{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-10T07:44:08.817963Z",
     "start_time": "2025-10-10T07:44:06.432308Z"
    }
   },
   "source": [
    "import concurrent.futures\n",
    "import requests\n",
    "import threading\n",
    "import time\n",
    "\n",
    "def download_one(url):\n",
    "    resp = requests.get(url)\n",
    "    print('Read {} from {}'.format(len(resp.content), url))\n",
    "\n",
    "\n",
    "def download_all(sites):\n",
    "    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n",
    "        to_do = []\n",
    "        for site in sites:\n",
    "            future = executor.submit(download_one, site)\n",
    "            to_do.append(future)\n",
    "\n",
    "        for future in concurrent.futures.as_completed(to_do):\n",
    "            future.result()\n",
    "\n",
    "def main():\n",
    "    sites = [\n",
    "        'https://en.wikipedia.org/wiki/Portal:Arts',\n",
    "        'https://en.wikipedia.org/wiki/Portal:History',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Society',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Biography',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Mathematics',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Technology',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Geography',\n",
    "        'https://en.wikipedia.org/wiki/Portal:Science',\n",
    "        'https://en.wikipedia.org/wiki/Computer_science',\n",
    "        'https://en.wikipedia.org/wiki/Python_(programming_language)',\n",
    "        'https://en.wikipedia.org/wiki/Java_(programming_language)',\n",
    "        'https://en.wikipedia.org/wiki/PHP',\n",
    "        'https://en.wikipedia.org/wiki/Node.js',\n",
    "        'https://en.wikipedia.org/wiki/The_C_Programming_Language',\n",
    "        'https://en.wikipedia.org/wiki/Go_(programming_language)'\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 92 from https://en.wikipedia.org/wiki/Portal:ArtsRead 92 from https://en.wikipedia.org/wiki/Portal:History\n",
      "\n",
      "Read 92 from https://en.wikipedia.org/wiki/Portal:Mathematics\n",
      "Read 92 from https://en.wikipedia.org/wiki/Portal:Biography\n",
      "Read 92 from https://en.wikipedia.org/wiki/Portal:Society\n",
      "Read 92 from https://en.wikipedia.org/wiki/Python_(programming_language)\n",
      "Read 92 from https://en.wikipedia.org/wiki/Portal:Technology\n",
      "Read 92 from https://en.wikipedia.org/wiki/Computer_science\n",
      "Read 92 from https://en.wikipedia.org/wiki/Portal:Science\n",
      "Read 92 from https://en.wikipedia.org/wiki/Portal:Geography\n",
      "Read 92 from https://en.wikipedia.org/wiki/Java_(programming_language)\n",
      "Read 92 from https://en.wikipedia.org/wiki/Node.js\n",
      "Read 92 from https://en.wikipedia.org/wiki/PHP\n",
      "Read 92 from https://en.wikipedia.org/wiki/The_C_Programming_Language\n",
      "Read 92 from https://en.wikipedia.org/wiki/Go_(programming_language)\n",
      "Download 15 sites in 2.3526674997992814 seconds\n"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
